﻿//
//  This code has been copied from the .NET framework.
// 
// 
// 

using System;
using System.Globalization;
using System.Text;

namespace T4UtilityBelt.UxDesigns.Controls
{
    public struct GridLength
    {
        private const double Default = 1;

        private double _unitValue;

        private GridUnitType _unitType;

        private readonly static GridLength s_auto = new GridLength(1, GridUnitType.Auto);

        public static GridLength Auto
        {
            get
            {
                return GridLength.s_auto;
            }
        }

        public GridUnitType GridUnitType
        {
            get
            {
                return this._unitType;
            }
        }

        public bool IsAbsolute
        {
            get
            {
                return this._unitType == GridUnitType.Pixel;
            }
        }

        public bool IsAuto
        {
            get
            {
                return this._unitType == GridUnitType.Auto;
            }
        }

        public bool IsStar
        {
            get
            {
                return this._unitType == GridUnitType.Star;
            }
        }

        public double Value
        {
            get
            {
                if (this._unitType == GridUnitType.Auto)
                {
                    return GridLength.s_auto._unitValue;
                }
                else
                {
                    return this._unitValue;
                }
            }
        }

        public GridLength(double pixels)
            : this(pixels, GridUnitType.Pixel)
        {
        }

        public GridLength(double value, GridUnitType type)
        {
            if (double.IsNaN(value) || double.IsInfinity(value) || value < 0)
            {
                throw new ArgumentException("InvalidArgument", "value");
            }
            else
            {
                if (type == GridUnitType.Auto || type == GridUnitType.Pixel || type == GridUnitType.Star)
                {
                    //GridLength gridLength = this;
                    double num;
                    if (type == GridUnitType.Auto)
                    {
                        num = 1;
                    }
                    else
                    {
                        num = value;
                    }
                    _unitValue = num;
                    this._unitType = type;
                    //return;
                }
                else
                {
                    throw new ArgumentException("InvalidArgument", "type");
                }
            }
        }

        public override bool Equals(object obj)
        {
            if (!(obj is GridLength))
            {
                return false;
            }
            else
            {
                GridLength gridLength = (GridLength)obj;
                return this == gridLength;
            }
        }

        public bool Equals(GridLength gridLength)
        {
            return this == gridLength;
        }

        //public override int GetHashCode()
        //{
        //    return (GridUnitType)((int)this._unitValue) + this._unitType;
        //}

        public static bool operator ==(GridLength left, GridLength right)
        {
            if (left.GridUnitType != right.GridUnitType)
            {
                return false;
            }
            else
            {
                return left.Value == right.Value;
            }
        }

        public static bool operator !=(GridLength left, GridLength right)
        {
            if (left.GridUnitType != right.GridUnitType)
            {
                return true;
            }
            else
            {
                return left.Value != right.Value;
            }
        }

        public override string ToString()
        {
            return this.ToString(CultureInfo.InvariantCulture);
        }

        internal string ToString(CultureInfo cultureInfo)
        {
            StringBuilder stringBuilder;
            //TokenizerHelper.GetNumericListSeparator(cultureInfo);
            if (this._unitType != GridUnitType.Auto)
            {
                if (this._unitType != GridUnitType.Pixel)
                {
                    stringBuilder = new StringBuilder(13);
                    stringBuilder.Append(Convert.ToString(this._unitValue, cultureInfo));
                    stringBuilder.Append("*");
                }
                else
                {
                    stringBuilder = new StringBuilder(12);
                    stringBuilder.Append(Convert.ToString(this._unitValue, cultureInfo));
                }
            }
            else
            {
                stringBuilder = new StringBuilder(4);
                stringBuilder.Append("Auto");
            }
            return stringBuilder.ToString();
        }
    }

}
